package l1j.server.server.model;

import java.util.logging.Level;
import java.util.logging.Logger;

import l1j.opqlo.Timer.Controler.Server.ServerWarExecutor;
import l1j.server.Thread.GeneralThreadPool;
import l1j.server.server.model.Instance.L1PcInstance;
import l1j.server.server.model.skill.L1SkillId;
import l1j.server.server.serverpackets.S_PinkName;

// Referenced classes of package l1j.server.server.model:
// L1PinkName

public class L1PinkName {
	private static final Logger _log = Logger.getLogger(L1PinkName.class.getName());

	private L1PinkName() {
	}
	
	private static class PinkNameTimer implements Runnable {
		private L1PcInstance _attacker = null;
		private int _i;
		public PinkNameTimer(final L1PcInstance attacker) {
			this._attacker = attacker;
		}

		// @Override
		@Override
		public void run() {
			try{
				while(this._attacker.isPinkName()){
					//			for (int i = 0; i < 60; i++) {
					if(this._attacker.getNetConnection() == null){
						return;
					}
					Thread.sleep(1000);
					_i++;
					// 死亡、または、相手を倒して赤ネームになったら終了
					if(this._attacker.getMapId() !=1813){//一般競技場
						if (this._attacker.isDead()) {
							break;
						}
						if (this._attacker.getLawful() < 0) {
							break;
						}
					}else if(_i >= 180){
						break;
					}
				}
			}catch(Exception e){
				_log.log(Level.SEVERE , e.getLocalizedMessage() , e);
			}finally{
				this.stopPinkName(this._attacker);
			}
		}
		private void stopPinkName(final L1PcInstance attacker) {
			try{
				attacker.sendPacketsAll(new S_PinkName(attacker.getId(), 0));
				attacker.setPinkName(false);
			}catch(Exception e){
				_log.log(Level.SEVERE , e.getLocalizedMessage() , e);
			}
		}
	}
	
		public static void onAction(final L1PcInstance pc, final L1Character cha) {
			if (pc == null || cha == null) {
				return;
			}

			if (!(cha instanceof L1PcInstance)) {
				return;
			}
			final L1PcInstance attacker = (L1PcInstance) cha;
			// 被攻擊者已在紅名狀態
			if (pc.isPinkName()) {
				return;
			}
			if (attacker.isPinkName()) {
				return;
			}
			
			if (pc.getId() == attacker.getId()) {
				return;
			}
			if (attacker.getFightId() == pc.getId()) {
				return;
			}
			// 被攻擊者在非一般區域
			if (pc.getZoneType() != 0) {
				return;
			}
			// 攻擊者在非一般區域
			if (attacker.getZoneType() != 0) {
				return;
			}
			

			boolean isNowWar = false;
			final int castleId = L1CastleLocation.getCastleIdByArea(pc);
			if (castleId != 0) {// 戰爭旗內
				isNowWar = ServerWarExecutor.get().isNowWar(castleId);
			}
			
			// 戰爭中
			if (isNowWar == true) {
				return;
			}
			// 被攻擊者正義質小於0
			if (pc.getLawful() < 0) {
				return;
			}
			
			attacker.setSkillEffect(L1SkillId.PC_IsPinkName, 180000);//opqlo 修正紫名更新問題
			attacker.sendPacketsAll(new S_PinkName(attacker.getId(),180));
			attacker.setPinkName(true);
			final PinkNameTimer pink = new PinkNameTimer(attacker);
			GeneralThreadPool.getInstance().execute(pink);
			
//			// pc, attacker共に青ネーム
//			if (pc.getLawful() >= 0 &&
//					!pc.isPinkName() 
//					&& attacker.getLawful() >= 0
//					&& !attacker.isPinkName()) {
//				if (pc.getZoneType() == 0 && // 共にノーマルゾーンで、戦争時間内で旗内でない
//						attacker.getZoneType() == 0 && isNowWar == false) {
//					attacker.setPinkName(true);
//					attacker.sendPackets(new S_PinkName(attacker.getId(), 60));
//					if (!attacker.isGmInvis()) {
//						attacker.broadcastPacketAll(new S_PinkName(attacker.getId(),60));
//					}
//					final PinkNameTimer pink = new PinkNameTimer(attacker);
//					GeneralThreadPool.getInstance().execute(pink);
//				}
//			}
		}
}
